﻿using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;

namespace IHISCETeim.Web.MachineSys.FTradeModel
{
    public partial class AddAdjustingPage : PageBase
    {
        protected override string DWZRef
        {
            get
            {
                return "AddAdjustingPage";
            }
        }

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }

        protected string JingXiaoShang
        {
            get
            {

                return GetRequestValue<string>("JingXiaoShang", "");
            }
        }

        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu2();
                
            }
        }

        private void addmenu2()
        {
            /*
             不同来源的收款记录 调帐，将生成不同的记录。
             */
            char[] caSplit = new char[] { ',' };
            string[] FCatheringIds = GetRequestValue<string>("FCatheringId", "").Split(caSplit);
            string[] FbalanceIds = GetRequestValue<string>("FbalanceId", "").Split(caSplit);
            string[] AdjustingMoney_Arr = GetRequestValue<string>("AdjustingMoney", "0").Split(caSplit);
            string[] oldAdjustingMoney_Arr = GetRequestValue<string>("oldAdjustingMoney", "0").Split(caSplit);

            //key=FCatheringId
            Dictionary<int, List<FTradeAdjustingTemp>> adObjs = new Dictionary<int, List<FTradeAdjustingTemp>>();
            int FCatheringId = 0;
            for (int rowIndex = 0; rowIndex < FCatheringIds.Count(); rowIndex++)
            {
                FCatheringId = Convert.ToInt32(FCatheringIds[rowIndex]);
                FTradeAdjustingTemp adtemp = new FTradeAdjustingTemp();
                adtemp.FCatheringId = FCatheringId;
                adtemp.FBalanceId = Convert.ToInt32(FbalanceIds[rowIndex]);
                adtemp.AdjustingMoney = Convert.ToDecimal(AdjustingMoney_Arr[rowIndex]);
                adtemp.OldAdjustingMoney = Convert.ToDecimal(oldAdjustingMoney_Arr[rowIndex]);

                if (adObjs.ContainsKey(FCatheringId))
                {
                    adObjs[FCatheringId].Add(adtemp);
                }
                else
                {
                    List<FTradeAdjustingTemp> flisttemp = new List<FTradeAdjustingTemp>();
                    flisttemp.Add(adtemp);

                    adObjs.Add(FCatheringId, flisttemp);
                }
            }
            //key=FCatheringId
            Dictionary<int, FTradeCatheringInfo> FCObjs = new Dictionary<int, FTradeCatheringInfo>();
            FTradeCatheringInfo objCat = new FTradeCatheringInfo();
            foreach (int fcid in adObjs.Keys)
            {
                FTradeCatheringInfo oldFCObj = FTradeCatheringController.GetById(fcid);
                //处理调帐金额
                Decimal tempJinE = 0m;
                foreach (FTradeAdjustingTemp itm in adObjs[fcid])
                {
                    tempJinE += itm.AdjustingMoney;
                }

                objCat = new FTradeCatheringInfo();
                objCat.FCTitle = string.Format("调帐:来自[{0}]",oldFCObj.FCTitle);
                objCat.ShoukuanRiQi = oldFCObj.ShoukuanRiQi;
                objCat.JingXiaoShang = JingXiaoShang;
                objCat.JinE = tempJinE;
                objCat.KuanXiangLeiXing = "调帐";
                objCat.RealName = User.RealName;
                objCat.IsAdjustable = false;
                objCat.State = "未冲完";
                objCat.Remark = oldFCObj.Remark;
                objCat.MoneyFrom = oldFCObj.MoneyFrom;
                objCat.SourceTag = "系统";
                objCat.oldKuanXiangLeiXing = oldFCObj.oldKuanXiangLeiXing;//原款项类型

                FCObjs.Add(fcid, objCat);
            }


            //
            bool msg = FTradeBalanceController.AddAdjusting2(FCObjs, adObjs);

            // AddLog(Permission.修改, obj, objTrade);

            if (msg)
            {
                if(FCObjs.Count>=2)
                {
                    string msg2 = "已成功调帐,但由于生成多条调帐记录,请手工选择要冲帐的调帐款.<br/>请手工关闭此调帐页面";
                    //string tabTitle = "收款及冲帐";
                    //string taburl = "FTradeModel/FTradeCatheringListEasyUI.aspx" ;
                    //string tabrelId = "FTradeCatheringListEasyUI";
                    //SuccessDialogOpenNavTab(msg2, tabTitle, taburl, tabrelId);
                    SuccessJson(msg2, DWZRef);
                }
                else
                {
                    //跳转参数
                    string msg2 = "已成功调帐,进入冲帐界面...";
                    string tabTitle = "冲帐";
                    string taburl = "FTradeModel/FTradeBalanceAdd.aspx?FCatheringId=" + objCat.FCatheringId.ToString();
                    string tabrelId = "FTradeBalanceAdd";
                    SuccessDialogOpenNavTab(msg2, tabTitle, taburl, tabrelId);
                }
                // SuccessJson("已调帐成功", "");
            }
            else
            {

                ErrorJson("调帐失败,相应记录同时有人调帐过，请确认");
            }

        }

        //private void addmenu()
        //{

        //    FTradeCatheringInfo objCat = new FTradeCatheringInfo();
        //    objCat.FCTitle = "调帐" + DateTime.Now.ToString("yyyy-MM-dd");
        //    objCat.ShoukuanRiQi = DateTime.Now;
        //    objCat.JingXiaoShang = JingXiaoShang;
        //    objCat.JinE = Convert.ToDecimal(GetRequestValue<Decimal>("AllBalance", 0));
        //    objCat.KuanXiangLeiXing = "调帐";
        //    objCat.RealName = User.RealName;
        //    objCat.IsAdjustable = false;
        //    objCat.State = "未冲完";
        //    objCat.Remark = "";
        //    objCat.SourceTag = "系统";


        //    char[] caSplit = new char[] { ',' };
        //    string[] FbalanceIds = GetRequestValue<string>("FbalanceId", "").Split(caSplit);
        //    string[] AdjustingMoney_Arr = GetRequestValue<string>("AdjustingMoney", "0").Split(caSplit);
        //    string[] oldAdjustingMoney_Arr = GetRequestValue<string>("oldAdjustingMoney", "0").Split(caSplit);

        //    List<int> list = new List<int>();
        //    List<decimal> AdjustingMoneys = new List<decimal>();
        //    List<decimal> oldAdjustingMoneys = new List<decimal>();

        //    for (int rowIndex = 0; rowIndex < FbalanceIds.Count(); rowIndex++)
        //    {
        //        list.Add(Convert.ToInt32(FbalanceIds[rowIndex]));
        //        AdjustingMoneys.Add(Convert.ToDecimal(AdjustingMoney_Arr[rowIndex]));
        //        oldAdjustingMoneys.Add(Convert.ToDecimal(oldAdjustingMoney_Arr[rowIndex]));
        //    }




        //    bool msg = FTradeBalanceController.AddAdjusting2(objCat, list, AdjustingMoneys, oldAdjustingMoneys);

        //    // AddLog(Permission.修改, obj, objTrade);

        //    if (msg)
        //    {
        //        //跳转参数
        //        string msg2 = "已成功调帐,进入冲帐界面...";
        //        string tabTitle = "冲帐";
        //        string taburl = "FTradeModel/FTradeBalanceAdd.aspx?FCatheringId=" + objCat.FCatheringId.ToString();
        //        string tabrelId = "FTradeBalanceAdd";
        //        SuccessDialogOpenNavTab(msg2, tabTitle, taburl, tabrelId);

        //       // SuccessJson("已调帐成功", "");
        //    }
        //    else
        //    {

        //        ErrorJson("调帐失败,相应记录同时有人调帐过，请确认");
        //    }
        //}
    }
}